package com.x.control;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.x.service.transactional.LocationService;

@Controller
@RequestMapping("/location")
public class LocationController {

	@Autowired(required = true)
	LocationService locationService;
	String[] columns = { "province", "city", "district" };

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");

		List<String[]> odrerPart = new ArrayList<>();
		List<String> wherePart = new ArrayList<>();

		for (int i = 0; i < columns.length; i++) {
			String orderColumn = request.getParameter("order[" + i
					+ "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] {
						columns[Integer.parseInt(orderColumn)], orderDir });
			}
		}
		for (int i = 0; i < columns.length; i++) {
			String searchable = request.getParameter("columns[" + i
					+ "][searchable]");
			String value = request.getParameter("columns[" + i
					+ "][search][value]");
			if (searchable == "false" || value.isEmpty()) {
				value = "";

			}
			wherePart.add(value);
		}

		Map<String, Object> result = locationService.list(startIndex, length,
				wherePart, odrerPart);
		result.put("draw", draw);
		return result;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	ModelAndView create(@RequestParam("province") String province,
			@RequestParam("city") String city,
			@RequestParam("district") String district) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("nav", "location");
		mv.setViewName("home");
		if (!locationService.create(province, city, district)) {
			mv.addObject("error", "create location fail: Duplicate Locations!");
		}
		return mv;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	String delete(@RequestParam("id") String id) {
		locationService.delete(id);
		return "redirect:/home/location";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	String update(@RequestParam("province") String province,
			@RequestParam("city") String city,
			@RequestParam("district") String district,
			@RequestParam("id") String id) {
		locationService.update(province, city, district, id);
		return "redirect:/home/location";
	}

	@RequestMapping(value = "/suggest/province")
	@ResponseBody
	public List<String> suggestProvince() {
		List<String> result = new ArrayList<>();
		result = locationService.getNodeByType("1", 0);
		return result;

	}

	@RequestMapping(value = "/suggest/city/{cityName}")
	@ResponseBody
	public List<String> suggestCity(
			@PathVariable(value = "cityName") String cityName) {
		List<String> result = new ArrayList<>();
		try {
			cityName = new String(cityName.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result = locationService.getNodeSuggest(cityName, "2");
		return result;
	}

	@RequestMapping(value = "/suggest/district/{districtName}")
	@ResponseBody
	public List<String> suggestDistrict(
			@PathVariable(value = "districtName") String districtName) {
		List<String> result = new ArrayList<>();
		try {
			districtName = new String(districtName.getBytes("ISO-8859-1"),
					"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result = locationService.getNodeSuggest(districtName, "3");
		return result;
	}
	
	@RequestMapping(value = "/select/province")
	@ResponseBody
	public  List<Map<String,String>>selectProvince() {		
		return locationService.getProvinceSelect();

	}
	
	@RequestMapping(value = "/select/city")
	@ResponseBody
	public  List<Map<String,String>>selectCity(@RequestParam("province") String provinceId) {		
		return locationService.getCitySelect(provinceId);

	}
	
	@RequestMapping(value = "/select/district")
	@ResponseBody
	public  List<Map<String,String>>selectDistrict(@RequestParam("city") String cityId) {		
		return locationService.getCitySelect(cityId);

	}
}
